home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #2 / Monster Media No. 2 (Monster Media)(1994).ISO / prog_gen / gcoope10.zip / ARRAY.C next >
Text File  |  1994-07-21  |  2KB  |  104 lines

  1. /*
  2.  
  3.     Array class definition for GCOOPE 1.0
  4.  
  5.     Compatible with experimental strong typing option.
  6.  
  7.     Designed 7/21/94 by Brian L. Price
  8.  
  9.     Released as Public Domain  July, 1994.
  10.  
  11.     note: the type word is a synonym for unsigned short int
  12.  
  13. */
  14.  
  15.  
  16. #define CLASS Array
  17.  
  18. #include "gcoope10.h"
  19. #include <mem.h>
  20. #include <stdio.h>
  21.  
  22. object CLASS;
  23.  
  24. extern object Dynmem;
  25.  
  26. typedef struct {
  27.     word    elemSize,
  28.         numElems;
  29.         } instVars;
  30.  
  31. USEGEN(addressOf);
  32. USEGEN(reSize);
  33. USEGEN(getElem);
  34. USEGEN(putElem);
  35. USEGEN(sizeOf);
  36. USEGEN(numElems);
  37.  
  38. cmethod object m4New(object instance, word elemSize, word numElems)
  39. {
  40. instVars * ivptr;
  41.  
  42. if(NULL==(ivptr=makeInst(&instance))) return 0;
  43. ivptr->elemSize=elemSize;
  44. ivptr->numElems=numElems;
  45. g(New)(ST(Dynmem), ivptr->elemSize * ivptr->numElems);
  46.  
  47. return instance;
  48. }
  49.  
  50.  
  51. imethod object m4reSize(object instance, word newNumElems)
  52. {
  53. instVars * ivptr;
  54.  
  55. if(NULL==(ivptr=getIVptr(instance))) return FUNCFAIL;
  56. ivptr->numElems=newNumElems;
  57. return g(GEN(reSize))(ST(Dynmem), ivptr->elemSize * newNumElems);
  58. }
  59.  
  60.  
  61. imethod object m4putElem(object instance, word elemNdx, void * newElem)
  62. {
  63. instVars * ivptr;
  64.  
  65. if(NULL==(ivptr=getIVptr(instance))
  66.     || ivptr->numElems <= elemNdx || NULL==newElem) return FUNCFAIL;
  67. memcpy(((char *) g(GEN(addressOf))(instance))
  68.     + elemNdx * ivptr->elemSize, newElem, ivptr->elemSize);
  69. return FUNCOKAY;
  70. }
  71.  
  72.  
  73. imethod void * m4getElem(object instance, word elemNdx)
  74. {
  75. instVars * ivptr;
  76.  
  77. if(NULL==(ivptr=getIVptr(instance)) || ivptr->numElems <= elemNdx)
  78.     return NULL;
  79. return ((char *) g(GEN(addressOf))(instance))+ elemNdx * ivptr->elemSize;
  80. }
  81.  
  82.  
  83. imethod word m4sizeOf(object instance)
  84. {
  85. return ((instVars *) getIVptr(instance))->elemSize;
  86. }
  87.  
  88.  
  89. imethod word m4numElems(object instance)
  90. {
  91. return ((instVars *) getIVptr(instance))->numElems;
  92. }
  93.  
  94.  
  95. CLASS_INSTALL
  96. {
  97. if(END==(CLASS=g(New)(Class, 0, sizeof(instVars), Dynmem, END))
  98.     || addGMthd(CLASS,New,(method) m4New) || ADDGM(reSize)
  99.     || ADDGM(putElem) || ADDGM(getElem) || ADDGM(sizeOf)
  100.     || ADDGM(numElems)) return FUNCFAIL;
  101. else return FUNCOKAY;
  102. }
  103.  
  104.